<script type="text/javascript" src="<?php echo ThemePath(); ?>/js/createClass.js"></script>
<script type="text/javascript" src="<?php echo ThemePath(); ?>/js/jquery.blockUI.js"></script>

<div class="blockPage">
    <div class="span12">	            	
        <ul class="breadcrumb">
            <li class="previous"><a href="<?php echo SITE_URL; ?>">Home</a> /</li>
            <li><a href="javascript:void(0)">Class Venue</a></li>
        </ul>
        <div class="common">
            <div class="whiteBg blueBor padding20px">
                <h5 class="fnt-size20">Create A Class</h5>
            </div>
        </div>

        <div class="common">                	
            <div class="createBox addmarginB20">                        
                <h4 class="txtfff fnt-size20"><span class="stepCount">Step 2</span>Class Venue Batch</h4>
            </div>                                                                                    
        </div>                                                                           

    </div>

    <div class="span3">
        <div class="tabBox">
            <ul class="innerTab">
                <?php
                $venueIndex = '';
                if (!empty($venuesWithClass)) {
                    for ($venueIndex = 0; $venueIndex < count($venuesWithClass); $venueIndex++) {
                        ?>
                        <li class="<?php echo ($venueIndex == 0) ? 'active' : ''; ?> add_venue_class_show_hide_<?php echo $venueIndex; ?>"><a href="javascript:void(0)" id="venue_class_show_hide_<?php echo$venueIndex; ?>" onclick="showVenueContent(this.id);">Class Venue <span class="createdVenueCount" id="create_venue_class_show_hide_<?php echo $venueIndex; ?>"> <?php echo $venueIndex + 1; ?> </span></a></li>
                        <?php
                    }
                } else {
                    ?>
                    <li class="active add_venue_class_show_hide_0"><a href="javascript:void(0)" id="venue_class_show_hide_0" onclick="showVenueContent(this.id);">Class Venue <span class="createdVenueCount" id="create_venue_class_show_hide_0"> 1 </span></a></li>
                <?php } ?></ul>
            <div class="clear"></div>
            <a href="javascript:void(0)"  class="btn addmarginB20" id="venue_<?php echo ($venueIndex == '') ? '0' : $venueIndex - 1; ?>" onclick="addVenue(this.id);">Add Venue</a>
        </div>
    </div>
    <div class="span9">
        <?php
        $form = $this->beginWidget('GxActiveForm', array(
            'id' => 'class-venue-form',
            'enableAjaxValidation' => true,
                ));
        ?>
        <div class="errorMessage" id="errorAnchor" style="display:none">Please Check all venue and batch something may be blank field.</div>
        <?php
        if (!empty($venuesWithClass)) {
            $this->renderPartial('_classvenueUpdate', array('model' => $model, 'modelClassBatch' => $modelClassBatch, 'venuesWithClass' => $venuesWithClass,
                'selectedVenue' => $selectedVenue, 'form' => $form));
        } else {
            ?>
            <div class="venueDetails">
                <div id="venue_count_0" class="venue_class_show_hide_0 venueContent">
                    <fieldset>
                        <div class="createBox addmarginB20">
                            <div class="padding20px">
                                <div class="formElements">
                                    <div class="col-2">
                                        <div class="common">
                                            <input type="hidden" name="venueIndex[]" value ="0">
                                            <input type="hidden" name="getVenueId" id="selectedVenueId">
                                            <input type="hidden" name="getCountry" id="getCountryId">
                                            <input type="hidden" id="getAllselectVenueVal" value="">
                                            <div class="col col1"> 
                                                <label>Select Venue </label>
                                                <em class="sprite question-sign" title="<?php echo Message::SELECT_VENUE; ?>">&nbsp;</em>
                                                <div class="clear"></div>
                                                <?php echo CHtml::dropDownList('ClassVenue[venueId][]', '', getVenueByUser($model), array("empty" => "Select venue", 'class' => 'selectedVenue', 'onchange' => 'getVanueDeatils(this.id);', 'id' => 'class_venue_venueid_0')); ?>
                                                <p id="error_class_venue_venueid_0" class="errorMessage" style="display:none">You have already selected this venue. So please select another venue!</p>
                                            </div>

                                            <div class="col col2">
                                                <?php echo $form->labelEx($model, 'name'); ?>
                                                <em class="sprite question-sign" title="<?php echo Message::VENUE_NAME; ?>">&nbsp;</em>
                                                <div class="clear"></div>
                                                <?php echo $form->textField($model, 'name[]', array('maxlength' => 45, 'id' => 'class_venue_name_0', 'class' => 'requiredfield field', 'onblur' => 'checkValueEmpty(this.id)')); ?>
                                                <div class="errorMessage" id="error_class_venue_name_0" style="display:none">Name cannot be blank.</div>
                                            </div>                                                         
                                            <div class="clear"></div>
                                        </div>

                                        <div class="common">
                                            <?php echo $form->labelEx($model, 'address_line1'); ?>
                                            <em class="sprite question-sign" title="<?php echo Message::VENUE_ADDRESS; ?>">&nbsp;</em>
                                            <div class="clear"></div>
                                            <?php echo $form->textField($model, 'address_line1[]', array('maxlength' => 45, 'id' => 'class_venue_address_line1_0', 'class' => 'requiredfield field large', 'onblur' => 'checkValueEmpty(this.id)')); ?>
                                            <div class="errorMessage" id="error_class_venue_address_line1_0" style="display:none">Address Line 1 cannot be blank.</div>
                                            <div class="clear"></div>
                                        </div>

                                        <div class="common">
                                            <?php echo $form->labelEx($model, 'address_line2'); ?>
                                            <div class="clear"></div>
                                            <?php echo $form->textField($model, 'address_line2[]', array('maxlength' => 45, 'id' => 'class_venue_address_line2_0', 'class' => 'field large')); ?>
                                            <div class="clear"></div>
                                        </div>

                                        <div class="common">
                                            <?php echo $form->labelEx($model, 'venue_locality'); ?>
                                            <em class="sprite question-sign" title="<?php echo Message::VENUE_LOCALITY; ?>">&nbsp;</em>
                                            <div class="clear"></div>
                                            <?php echo $form->textField($model, 'venue_locality[]', array('maxlength' => 45, 'id' => 'class_venue_venue_locality_0', 'class' => 'requiredfield field large', 'onkeyup' => "getLocality(this.id)",'onpaste'=>"return false;")); ?>
                                            <div class="errorMessage" id="error_class_venue_venue_locality_0" style="display:none">Locality cannot be blank.</div>
                                            <div class="errorMessage" id="error_local_class_venue_venue_locality_0"></div>
                                            <div class="note" > Note:<?php echo Message::LOCALITY_HELP; ?></div>
                                            <div class="clear"></div>
                                        </div>


                                        <div class="common">
                                            <div class="col col1">
                                                <?php echo $form->labelEx($model, 'zip_code'); ?>
                                                <div class="clear"></div>
                                                <?php echo $form->textField($model, 'zip_code[]', array('maxlength' => 6, 'id' => 'class_venue_zip_code_0', 'class' => 'field requiredfield', 'onblur' => 'checkValueEmpty(this.id)')); ?>
                                                <div class="errorMessage" id="error_class_venue_zip_code_0" style="display:none">Zip Code cannot be blank.</div>
                                            </div>

                                            <div class="col col2">
                                                <?php echo $form->labelEx($model, 'country_id'); ?>
                                                <div class="clear"></div>
                                                <?php echo CHtml::dropDownList('ClassVenue[country_id][]', SkillConfig::ACTIVE_COUNTRY, GxHtml::listData(Country::model()->findAllByAttributes(array('is_active' => SkillConfig::STATUS_ACTIVE, 'id' => SkillConfig::ACTIVE_COUNTRY)), 'id', 'name'), array('id' => 'class_venue_country_id_0', 'onchange' => 'getSelectedState(this.id)')); ?>
                                                <div class="errorMessage" id="error_class_venue_country_id_0" style="display:none">Country Name cannot be blank.</div>
                                            </div>                                                         
                                            <div class="clear"></div>
                                        </div>

                                        <div class="common">
                                            <div class="col col1">
                                                <?php echo $form->labelEx($model, 'state_id'); ?>
                                                <div class="clear"></div>
                                                <?php echo CHtml::dropDownList('ClassVenue[state_id][]', SkillConfig::ACTIVE_STATE, GxHtml::listData(State::model()->findAllByAttributes(array('is_active' => SkillConfig::STATUS_ACTIVE, 'id' => SkillConfig::ACTIVE_STATE)), 'id', 'name'), array('id' => 'class_venue_state_id_0', 'onchange' => 'getSelectedCity(this.id)')); ?>
                                                <div class="errorMessage" id="error_class_venue_state_id_0" style="display:none">State Name cannot be blank.</div>
                                            </div>

                                            <div class="col col2">
                                                <?php echo $form->labelEx($model, 'city_id'); ?>
                                                <div class="clear"></div>
                                                <?php echo CHtml::dropDownList('ClassVenue[city_id][]', '1', GxHtml::listData(City::model()->findAllByAttributes(array('status' => SkillConfig::STATUS_ACTIVE)), 'id', 'city_name'), array('id' => 'class_venue_city_id_0')); ?>
                                                <div class="errorMessage" id="error_class_venue_city_id_0" style="display:none">City Name cannot be blank.</div>
                                            </div>                                                         
                                            <div class="clear"></div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <?php /* .......................         Here is the batch code start................ */ ?>
                        <div class ="addbatch_venue_0_batch">
                            <?php $this->renderPartial('/classBatch/_form', array('form' => $form, 'model' => $modelClassBatch)); ?>
                        </div> 

                        <div class="addOpt addmarginB20">
                            <div class="padding20px">
                                <div class="floatL"><h4 class="txttTransformNorml txtfff">Batch Details</h4></div>
                                <div class="floatR"><a href="javascript:void(0)" class="btn-green" id="venue_0_batch_0" onclick="addBatch(this.id);">Add Batch</a></div>
                                <div class="clear"></div>
                            </div>    
                        </div>
                        <?php /* .......................         Here is the batch code end................ */ ?>
                    </fieldset></div>
            </div>
        <?php } ?>
        <div class="createBox addmarginB20">                         
            <div class="padding20px">

                <div class="dash-borB height1px addmarginB20"></div>
                <div class="formElements">
                    <div class="col-2">
                        <div class="common borB">
                            <label>Other Information</label>
                            <div class="clear"></div>
                            <textarea name="Classes[class_other_information]" id="Classes_class_other_information"><?php echo $classinfo; ?></textarea>
                            <div class="clear"></div>
                        </div>

                        <?php $this->renderPartial('/ticketBooked/_form', array('form' => $form, 'type' => 'class', 'classId' => $classId)); ?>


                    </div>

                </div>
            </div> 	
        </div>


        <div class="clear"></div>

        <div class="common">
            <p class="alignR">
                <input type="button" class="btn twoBtn" value="Back" id="<?php echo $authKey; ?>" onclick="setAuthKey(this.id, 'Classes/Update')">
                <?php echo GxHtml::submitButton(Yii::t('app', 'Save & Continue'), array('id' => 'save_class', 'class' => 'btn')); ?></p>
        </div> 

        <?php $this->endWidget(); ?>
    </div> </div> 
<script>
    function getVenueName(venueId) {
        imageloding();
        $("#selectedVenueId").val(venueId);
        $.post("<?php echo Yii::app()->createUrl("classVenue/getAllVenue"); ?>", {venueId: venueId}, function(result) {
            var venueId = $("#selectedVenueId").val();
            $("#" + venueId).html(result);
        });
    }
    function getAllCountry(countryId) {
        $("#getCountryId").val(countryId);
        $.post("<?php echo Yii::app()->createUrl("classVenue/getAllCountry"); ?>", {countryId: countryId}, function(result) {
            var countryId = $("#getCountryId").val();
            $("#" + countryId).html(result);
            getSelectedState(countryId);
        });
    }

    function getSelectedState(countryId) {
        var stateId = countryId.split("_");
        $("#getCountryId").val(stateId[4]);
        var countryId = $("#" + countryId).val();
        $.post("<?php echo Yii::app()->createUrl("classVenue/dynamicState"); ?>", {countryId: countryId}, function(result) {
            var stateId = $("#getCountryId").val();
            $("#class_venue_state_id_" + stateId).html(result);
            getSelectedCity("class_venue_state_id_" + stateId);
        });
    }
    function getSelectedCity(stateid) {
        var stateId = stateid.split("_");
        $("#getCountryId").val(stateId[4]);
        var stateId = $("#" + stateid).val();
        $.post("<?php echo Yii::app()->createUrl("classVenue/dynamicCity"); ?>", {stateId: stateId}, function(result) {
            var cityId = $("#getCountryId").val();
            $("#class_venue_city_id_" + cityId).html(result);
            $(document).ajaxStop($.unblockUI); 
        });
    }


    function updateTextArea() {
        var allVals = [];
        $('.selectedVenue').each(function() {
            allVals.push($(this).val());
        });
        $('#getAllselectVenueVal').val(allVals)
    }

    function getVanueDeatils(venueId) {
        var venueIndex = venueId.split("_");
        $("#getCountryId").val(venueIndex[3]);
        var venueIndex = $("#" + venueId).val();
        var venueIndexVal = $("#getCountryId").val();
        updateTextArea(venueIndex);
        if (venueIndex == '') {
            removeVanueValue(venueIndexVal)
        }
        else {
            num = 0;
            var selectedValue = $("#getAllselectVenueVal").val();
            var array = selectedValue.split(",");
            for (i = 0; i < array.length; i++)
                (array[i] == venueIndex) ? num++ : "";
            if (num == 1)
            {
                getVenueDetailsById(venueIndex);
                $("#error_class_venue_venueid_" + venueIndexVal).css("display", "none");
            }
            else {
                removeVanueValue(venueIndexVal);
                $("#error_class_venue_venueid_" + venueIndexVal).css("display", "block");
                $("#" + venueId).val('');
            }


        }
    }
    function getVenueDetailsById(venueIndex) {
        $.post("<?php echo Yii::app()->createUrl("classVenue/SelectedVenue"); ?>", {venueId: venueIndex,user_id:1}, function(result) {
            var obj = jQuery.parseJSON(result);
            var venueId = $("#getCountryId").val();
            getSelectedState("class_venue_country_id_" + venueId);
            $("#class_venue_name_" + venueId).val('' + obj.name);
            $("#class_venue_address_line1_" + venueId).val('' + obj.address_line1);
            $("#class_venue_address_line2_" + venueId).val('' + obj.address_line2);
            $("#class_venue_venue_locality_" + venueId).val('' + obj.venue_locality);
            $("#class_venue_zip_code_" + venueId).val('' + obj.zip_code);
            $("#class_venue_country_id_" + venueId).val('' + obj.country_id);
            $("#class_venue_state_id_" + venueId).val('' + obj.state_id);
            $("#class_venue_city_id_" + venueId).val('' + obj.city_id);
            $("#class_venue_name_" + venueId).attr("readonly", "readonly");
            $("#class_venue_address_line1_" + venueId).attr("readonly", "readonly");
            $("#class_venue_address_line2_" + venueId).attr("readonly", "readonly");
            $("#class_venue_venue_locality_" + venueId).attr("readonly", "readonly");
            $("#class_venue_zip_code_" + venueId).attr("readonly", "readonly");
            $("#error_class_venue_name_" + venueId).hide();
            $("#error_class_venue_address_line1_" + venueId).hide();
            $("#error_class_venue_address_line2_" + venueId).hide();
            $("#error_class_venue_zip_code_" + venueId).hide();
        });
    }

    function removeVanueValue(venueId) {
        $("#class_venue_name_" + venueId).removeAttr("readonly");
        $("#class_venue_address_line1_" + venueId).removeAttr("readonly");
        $("#class_venue_address_line2_" + venueId).removeAttr("readonly");
        $("#class_venue_zip_code_" + venueId).removeAttr("readonly");
        $("#class_venue_venue_locality_" + venueId).removeAttr("readonly");
        $("#class_venue_name_" + venueId).val('');
        $("#class_venue_address_line1_" + venueId).val('');
        $("#class_venue_address_line2_" + venueId).val('');
        $("#class_venue_venue_locality_" + venueId).val('');
        $("#class_venue_zip_code_" + venueId).val('');
    }

    $('#save_class').click(function(e) {
        var isValid = true;
        $('.requiredfield').each(function() {
            if ($.trim($(this).val()) == '') {              
                $(".venueContent").show();
                $(".content").show();
                $(".contenttoggleClass").show();
                var errorid = $('#' + this.id).next('div').attr("id");
                $("#" + errorid).show();
                isValid = false;
                window.location = '#errorAnchor';
                $("#errorAnchor").show();

            } else {
                var errorid = $('#' + this.id).next('div').attr("id");
                $("#" + errorid).hide();
                $("#errorAnchor").hide();

            }
        });
        $('.hastimePicker').each(function() {
            timeCheck(this.id);


        });
        if (isValid == false)
            e.preventDefault();


    });

    $(document).ready(function() {
        $(".dynamicclass").removeAttr('id');
        $('.DateTimePicker').on('click', function() {
            $(this).datetimepicker({
                stepMinute: 15
            }).datetimepicker('show');
        });

    });  
    
    
    function getLocality(id){
        var cache = {};
        $( "#"+id ).autocomplete({
            minLength: 2,
            maxLength: 2,
            source: function( request, response ) {
                var term = request.term;
                if ( term in cache ) {
                    response( cache[ term ] );
                    return;
                }
                $.getJSON( "<?php echo Yii::app()->createUrl("classVenue/getLocality"); ?>", request, function( data, status, xhr ) {
                    if(data!=0){
                        cache[ term ] = data;                     
                        response( data );}
                    else{
                        var k= $('#' + id).next('div').next('div').attr("id");
                        $("#"+k).text('Locality not found in database.this locality will approved.');
                        $("#"+id).focus();}
                });
            }
        });
    }

</script>
<style>
    .ui-autocomplete {
        max-height: 100px;
        overflow-y: auto;
        /* prevent horizontal scrollbar */
        overflow-x: hidden;
    }
    /* IE 6 doesn't support max-height
     * we use height instead, but this forces the menu to always be this tall
    */
    * html .ui-autocomplete {
        height: 100px;
    }
</style>
